import os
from  tkinter  import  *
from  tkinter  import filedialog,messagebox
a = None
def saveasfile():
    global a
    a = filedialog.asksaveasfilename()
    if a:
        open(a,"w",encoding="utf-8").write(in_t.get(0.0,END))
        save.place(x=1180, y=140)
    else:
        messagebox.showinfo("","用户取消保存！")
def openfile():
    global a
    a = filedialog.askopenfilename()
    if a:
        try:
            in_t.delete(0.0, END)
            in_t.insert(0.0,chars=open(a,"r",encoding="utf-8").read())
            save.place(x=1180,y=140)
            if a.split(".")[1]=="py":
                run.place(x=1100,y=200)
            else :
                run.place(x=9999,y=9999)
        except UnicodeDecodeError:
            messagebox.showerror("","")
def savefile():
    global  a
    f = open(a,"w",encoding="utf-8")
    f.write(in_t.get(0.0,END))
def run_with_python():
    messagebox.showinfo("","请转至命令行！")
    os.system("python {}".format(a))
windows = Tk()
windows.iconphoto(0, PhotoImage(file="../../../cd/pinux/icon.png"))
windows.geometry("1280x600")
windows.configure(bg="white")
windows.title("SmallText")
fn = ("Consolas",12)
in_t = Text(windows)
in_t.configure(bg="white",font=fn)
in_t.pack(ipadx=1280,ipady=windows.winfo_height())
saveas = Button(windows,text="Save As",command=saveasfile)
saveas.place(x=1180,y=20)
save = Button(windows,text="Save")
opens = Button(windows,text="Open", command=openfile)
opens.place(x=1180, y=80)
run = Button(windows,text="Run With Python",command=run_with_python)
windows.mainloop()
